package com.songcha.module_bookreader.ui.activity.sdcard_novel_select

import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.songcha.library_base.compose.BaseComposeMvvmActivity
import com.songcha.library_business.bean.book.BookBean
import com.songcha.module_bookreader.ui.activity.reader.BookReadActivity

class SDCardNovelSelectActivity:BaseComposeMvvmActivity<SDCardNovelSelectViewModel>() {

    override fun onGetData() {
        viewModel.getData()
    }

    @Composable
    override fun ComposeRootView() {
        val novelList by viewModel.novelList.collectAsState()

        LazyColumn (
            modifier = Modifier.fillMaxSize()
                .background(colorResource(com.songcha.library_common.R.color.page_background))
        ){
            itemsIndexed(novelList){index,item->
                Box(
                    modifier = Modifier.padding(bottom = 10.dp)
                        .fillMaxSize()
                        .height(50.dp)
                        .background(Color.White)
                        .clickable {
                            onBookItemClick(item)
                        },
                    contentAlignment = Alignment.Center
                ){
                    Text(
                        modifier = Modifier
                            .padding(10.dp,0.dp)
                            .fillMaxWidth(),
                        text = item,
                        fontSize = 16.sp,
                        color = Color.Black
                    )
                }
            }
        }
    }

    private fun onBookItemClick(item:String){
        val book = BookBean()
        book.id = item
        book.bookName = item
        val path = getExternalCacheDir()!!.getAbsolutePath() + "/${item}.txt"
        book.path = path
        book.isFromSDCard = true
        val intent = Intent(context, BookReadActivity::class.java)
        intent.putExtra("book", book)
        startActivity(intent)
    }
}